package com.yfp.machine.controller;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.machine.domain.Ad;
import com.yfp.machine.domain.dto.AdDto;
import com.yfp.machine.domain.vo.AdResourceVo;
import com.yfp.machine.service.AdService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


/**
 * (Ad)表控制层
 *
 *
 * @since 2023-12-28 14:55:50
 */
@RestController
@RequestMapping("ad")
@Tag(name = "广告相关管理")
public class AdController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private AdService adService;


    /**
     * 分页查询
     *
     * @param ad 筛选条件
     * @return 查询结果
     */
    @GetMapping
    @Operation(summary = "分页查询")
    public TableDataInfo queryAdByPage(Ad ad, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return getDatableByStream(this.adService.queryAdAll(ad), pageNum, pageSize);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @Operation(summary = "通过主键查询单条数据")
    public R<AdDto> queryById(@PathVariable("id") String id) {
        return R.ok(this.adService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param adResourceVo 实体
     * @return 新增结果
     */
    @PostMapping
    @Operation(summary = "新增广告")
    @Log(title = "广告管理", businessType = BusinessType.INSERT)
    public R add(@RequestBody AdResourceVo adResourceVo) {
        return this.adService.addAdResource(adResourceVo) ? R.ok() : R.fail();
    }

    /**
     * 编辑数据
     *
     * @param adResourceVo 实体
     * @return 编辑结果
     */
    @PutMapping
    @Operation(summary = "编辑广告")
    @Log(title = "编辑广告", businessType = BusinessType.UPDATE)
    public R edit(@RequestBody AdResourceVo adResourceVo) {
        return this.adService.updateAdResourceVo(adResourceVo) ? R.ok() : R.fail();
    }

    @GetMapping("del/{adId}")
    @Operation(summary = "删除广告")
    @Log(title = "删除广告", businessType = BusinessType.DELETE)
    public R delete(@PathVariable("adId") String adId) {
        return this.adService.deleteAd(adId) ? R.ok() : R.fail();
    }

}

